/**
 * 
 */
package com.p2p.front.component.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.p2p.base.bizInterface.ChangeNotify;
import com.p2p.base.util.ChangeNotifyUtil;
import com.p2p.front.component.MemberSetting;
import com.p2p.front.dal.model.SysConfig;
import com.p2p.front.service.SysConfigService;

/**
 * @author ruiqi
 *
 */
@Component("memberSetting")
public class MemberSettingImpl implements MemberSetting,ChangeNotify{

	private final Logger logger = LoggerFactory.getLogger(MemberSettingImpl.class);
	
	/**
	 * 等级对应的积分要求
	 */
	private Map<Integer,Integer> memberLevelPointMap = new HashMap<Integer,Integer>();
	
	/**
	 * 等级对应的名字
	 */
	private Map<Integer,String> memberLevelNameMap = new HashMap<Integer,String>();
	
	private Map<Integer,Double> memberLevelInterestChargesRateMap = new HashMap<Integer,Double>();
	
	
	@Resource
	private SysConfigService sysConfigService;
	
	@PostConstruct  
	@Override
	public void initMemberSetting() {
		SysConfig sysConfig = this.sysConfigService.getByKey(SysConfig.ConfigKeyEnum.MemberLevel);
		if(sysConfig==null){
			throw new RuntimeException("sys config "+SysConfig.ConfigKeyEnum.MemberLevel.name()+" is null ");
		}
		String value = sysConfig.getValue();
		String[] levelStrArray = value.split(",");
		for(String levelStr:levelStrArray){
			String[] levelArray= levelStr.split(":");
			Integer level = Integer.valueOf(levelArray[0]);
			Integer levelPoint = Integer.valueOf(levelArray[1]);
			Double levelInterestChargesRateMap = Double.valueOf(levelArray[3]);
			memberLevelPointMap.put(level, levelPoint);
			memberLevelNameMap.put(level, levelArray[2]);
			memberLevelInterestChargesRateMap.put(level, levelInterestChargesRateMap);
		}
	}
	
	@PostConstruct
	@Override
	public void addListener() {
		ChangeNotifyUtil.addListener(SysConfigService.notifySubject, this);
	}
	
	@Override
	public void change() {
		logger.info("initMemberSetting");
		this.initMemberSetting();
	}

	@Override
	public Map<Integer, String> getMemberLevelNameMap() {
		// TODO Auto-generated method stub
		return this.memberLevelNameMap;
	}

	@Override
	public String getMemberLevelName(int level) {
		// TODO Auto-generated method stub
		return this.memberLevelNameMap.get(level);
	}
	@Override
	public Integer getMemberLevelPoint(int level) {
		// TODO Auto-generated method stub
		return this.memberLevelPointMap.get(level);
	}
	
	public Double getMemberLevelChargeRate(int level){
		return this.memberLevelInterestChargesRateMap.get(level);
	}
}
